# SETWD ===========================
rm(list=ls())


# LIBRARIES ===========================

library(tidyverse)
library(foreign)
library(stargazer)
library(lmtest)
library(multiwayvcov)

# IMPORT DATA ===========================

dfgi <- read.csv("data/sve-08_04_24.csv",stringsAsFactors = F)

# COUNTRY FE ===========================

## TAB A16. Logit - Country Fixed Effects ----

m1 <- (glm(erosion.strict ~ gini_disp + country.name, data=dfgi, family=binomial))
summary(m1)
nobs(m1)

m2 <- (glm(erosion.strict ~ gini_disp + log(gdppc) + country.name, 
            data=dfgi, family=binomial))
summary(m2)
nobs(m2)

m3 <- (glm(erosion.strict ~ gini_disp + log(gdppc) + year + country.name, 
            data=dfgi, family=binomial))
summary(m3)
nobs(m3)

m4 <- (glm(erosion.strict ~ gini_disp + year + country.name, 
            data=dfgi, family=binomial))
summary(m4)
nobs(m4)

## FIG A5. Inequality Trend during Erosion in Bolivia ----

ggplot(dfgi[dfgi$country.name=="Bolivia",],aes(x=year,y=gini_disp))+
  geom_line()+
  geom_line(data=dfgi[dfgi$country.name=="Bolivia"&dfgi$erosion.strict==1,],
            color="red")+
  xlab("Year")+
  ylab("Post-fisc Gini")+
  facet_wrap(~country.name)+
  theme_bw()

ggsave("figures/gini-trend-bolivia.png",width=3.5,height=2.5)

## FIG A6. Inequality Trends during Erosion

ggplot(dfgi[dfgi$country.name=="South Africa",],aes(x=year,y=gini_disp))+
  geom_line()+
  geom_line(data=dfgi[dfgi$country.name=="South Africa"&dfgi$erosion.strict==1,],
            color="red")+
  xlab("Year")+
  ylab("Post-fisc Gini")+
  facet_wrap(~country.name)+
  theme_bw()

ggsave("figures/gini-trend-sa.png",width=3.5,height=2.5)


ggplot(dfgi[dfgi$country.name=="North Macedonia",],aes(x=year,y=gini_disp))+
  geom_line()+
  geom_line(data=dfgi[dfgi$country.name=="North Macedonia"&dfgi$erosion.strict==1,],
            color="red")+
  xlab("Year")+
  ylab("Post-fisc Gini")+
  facet_wrap(~country.name)+
  theme_bw()

ggsave("figures/gini-trend-macedonia.png",width=3.5,height=2.5)



ggplot(dfgi[dfgi$country.name=="India",],aes(x=year,y=gini_disp))+
  geom_line()+
  geom_line(data=dfgi[dfgi$country.name=="India"&dfgi$erosion.strict==1,],
            color="red")+
  xlab("Year")+
  ylab("Post-fisc Gini")+
  facet_wrap(~country.name)+
  theme_bw()

ggsave("figures/gini-trend-india.png",width=3.5,height=2.5)


## DESCRIPTIVE STATS ----

# gdp consistently increasing
ggplot(dfgi,aes(x=year,y=gdppc))+
  geom_line()+
  facet_wrap(~country.name)+
  theme_bw()

# gini not consistently decreasing
ggplot(dfgi,aes(x=year,y=gini_disp))+
  geom_line()+
  facet_wrap(~country.name)+
  theme_bw()

# bolivia gini:
dfgi%>%
  filter(country.name=="Bolivia")%>%
  dplyr::select(year,gini_disp,erosion.strict)
# average change during erosion:
(52.2-40.7)/(2019-2005)

# n macedonia gini:
dfgi%>%
  filter(country.name=="North Macedonia")%>%
  dplyr::select(year,gini_disp,erosion.strict)

# s africa gini:
dfgi%>%
  filter(country.name=="South Africa")%>%
  dplyr::select(year,gini_disp,erosion.strict)

# india gini:
dfgi%>%
  filter(country.name=="India")%>%
  dplyr::select(year,gini_disp,erosion.strict)  

# botswana gini:
dfgi%>%
  filter(country.name=="Botswana")%>%
  dplyr::select(year,gini_disp,erosion.strict)  

# us gini:
dfgi%>%
  filter(country.name=="United States")%>%
  dplyr::select(year,gini_disp,erosion.strict)  

# hungary gini:
dfgi%>%
  filter(country.name=="Hungary")%>%
  dplyr::select(year,gini_disp,erosion.strict)  





